package com.example.web.demos.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.web.demos.domain.dto.PrometheusResponse;
import com.example.web.demos.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * @author: scout.du
 * @date: 2024/06/19 17:57
 **/
@RestController
@RequestMapping(value = {"/prometheus"})
public class PrometheusController {

    @Autowired
    RestTemplate restTemplate;





    @PostMapping("/query")
    public PrometheusResponse query(@RequestParam String query){

        String url = "http://49.235.136.22:9090/api/v1/query?query=";
        String path = url + query;
        String forObject = restTemplate.getForObject(path, String.class);
        PrometheusResponse prometheusResponse = JSONObject.parseObject(forObject, PrometheusResponse.class);
        PrometheusResponse.convert(prometheusResponse);

        return prometheusResponse;

    }

    /*
    *  2024-09-09 13:30:00:000
    * start yyyy-MM-dd HH:mm:ss:SSS
    * start yyyy-MM-dd HH:mm:ss:SSS
    *
    * */
    @PostMapping("/query_range")
    public PrometheusResponse queryRange(@RequestParam String query,@RequestParam String start,@RequestParam String end,@RequestParam String step){

        String url = "http://49.235.136.22:9090/api/v1/query_range";

        start = String.valueOf(DateUtils.parseToLong(start, DateUtils.STANDARD_MILL));
        int startLength = start.length();
        String start1 = start.substring(0,startLength-3) + "." + start.substring(startLength-3);

        end = String.valueOf(DateUtils.parseToLong(end, DateUtils.STANDARD_MILL));
        int endLength = end.length();
        String end1 = end.substring(0,endLength-3) + "." + end.substring(endLength-3);


        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.put("query", List.of(query));
        map.put("start", List.of(start1));
        map.put("end", List.of(end1));
        map.put("step", List.of(step));
        PrometheusResponse prometheusResponse = restTemplate.postForObject(url, map, PrometheusResponse.class);
        PrometheusResponse.convert(prometheusResponse);

        return prometheusResponse;

    }


    @GetMapping("/getTimeStrs")
    public Map<String,String> getTimeStrs(@RequestParam String timestamps){

        String[] split = timestamps.split(",");
        HashMap<String,String> map = new LinkedHashMap<>();
        for (String timestamp : split) {
            String replace = timestamp.replace(".", "");
            map.put(timestamp,DateUtils.format(Long.valueOf(replace),DateUtils.STANDARD_MILL));
        }

        return map;
    }

    @GetMapping("/getTimeStamps")
    public Map<String,String> getTimeStamps(@RequestParam String timeStrs){

        String[] split = timeStrs.split(",");
        HashMap<String,String> map = new LinkedHashMap<>();

        for (String timeStr : split) {
            Long stamp = DateUtils.parseToLong(timeStr, DateUtils.STANDARD_MILL);
            String timestamp = String.valueOf(stamp);
            int length = timestamp.length();
            timestamp = timestamp.substring(0,length-3) + "." + timestamp.substring(length-3);
            map.put(timeStr,timestamp);
        }

        return map;
    }

}
